//
//  PriceSelectorViewController.m
//  CheXunWang
//
//  Created by zhu lin on 12/14/11.
//  Copyright 2011 Zhulin. All rights reserved.
//

#import "PriceSelectorViewController.h"
#import "HomeViewController.h"
#import "SeriesViewController.h"

#define kPriceScopeCount		9
#define kPriceScopeLableTag		1001

@interface PriceSelectorViewController()
@property (nonatomic, retain) NSArray *priceScopeArray;
@property (nonatomic, retain) NSArray *priceScopeValueArray;

- (void)showHomeView:(id)sender;
@end

@implementation PriceSelectorViewController
@synthesize priceScopeArray;
@synthesize priceScopeValueArray;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.priceScopeArray = [NSArray arrayWithObjects:@"5万以下", @"5万-8万", @"8万-11万", @"11万-15万", @"15万-20万", @"20万-25万", @"25万-35万", @"35万-50万", @"50万以上",nil];
	self.priceScopeValueArray = [NSArray arrayWithObjects:@"0-5", @"5-8", @"8-11", @"11-15", @"15-20", @"20-25", @"25-35", @"35-50", @"50-100000", nil];

	// set navigationbar
    self.title = @"价格筛选";
    self.navigationItem.hidesBackButton = YES;
	
	UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
	homeButton.frame = CGRectMake(0.0f, 8.0f, 87.0f, 27.0f);
	[homeButton addTarget:self action:@selector(showHomeView:) forControlEvents:UIControlEventTouchUpInside];
	[homeButton setImage:[UIImage imageNamed:@"ios_common_titlebar_logo.png"] forState:UIControlStateNormal];
	self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:homeButton];
	
	self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 12.0f)];
	self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 12.0f)];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return kPriceScopeCount;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return (40 + 8);
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		
		//cell.contentView.frame = CGRectMake(12.0f, 4.0f, 296.0f, 40.0f);
		
		// bg image
		UIView *bgView = [[UIView alloc] init];
		UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ios_price_filter_item_normal.png"]];
		bgImageView.frame = CGRectMake(12.0f, 4.0f, 296.0f, 40.0f);
		[bgView addSubview:bgImageView];
		[cell setBackgroundView:bgView];
		[bgImageView release];
		[bgView release];
		
		UIView *selectedBgView = [[UIView alloc] init];
		UIImageView *selectedBgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ios_price_filter_item_pressed.png"]];
		selectedBgImageView.frame = CGRectMake(12.0f, 4.0f, 296.0f, 40.0f);
		[selectedBgView addSubview:selectedBgImageView];
		[cell setSelectedBackgroundView:selectedBgView];
		[selectedBgImageView release];
		[selectedBgView release];
		
		// price label
		UILabel *priceScopeLabel = [[UILabel alloc] initWithFrame:CGRectMake(49.0f, 10.0f, 200.0f, 25.0f)];
		priceScopeLabel.font = [UIFont systemFontOfSize:18.0f];
		priceScopeLabel.backgroundColor = [UIColor clearColor];
		priceScopeLabel.textColor = [UIColor colorWithRed:59.0/255.0 green:59.0/255.0 blue:59.0/255.0 alpha:1.0];
		priceScopeLabel.tag = kPriceScopeLableTag;
		[cell.contentView addSubview:priceScopeLabel];
		[priceScopeLabel release];
		
		// accessor
		UIImageView *accessoryImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ios_price_filter_item_arrow.png"]];
		accessoryImage.frame = CGRectMake(284.0f, 20.0f, 7.0f, 11.0f);
		[cell.contentView addSubview:accessoryImage];
		[accessoryImage release];
		//cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ios_price_filter_item_arrow.png"]];
    }
    
    // Configure the cell...
	UILabel *priceScopeLabel = (UILabel *)[cell.contentView viewWithTag:kPriceScopeLableTag];
	priceScopeLabel.text = [self.priceScopeArray objectAtIndex:indexPath.row];
    
    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
	SeriesViewController *seriesVC = [[SeriesViewController alloc] initWithNibName:@"SeriesViewController" bundle:nil];
	seriesVC.title = (NSString *)[self.priceScopeArray objectAtIndex:indexPath.row];
	seriesVC.backType = ZLBackButtonTypeScope;
	seriesVC.scopeValue = [self.priceScopeValueArray objectAtIndex:indexPath.row];
	
	//
	[self.navigationController pushViewController:seriesVC animated:YES];
	[seriesVC getSeriesByScope:[self.priceScopeValueArray objectAtIndex:indexPath.row]];
	[seriesVC release];
	
	[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
	
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[priceScopeArray release];
	[priceScopeValueArray release];
    [super dealloc];
}

#pragma mark -
#pragma mark actions
- (void) showHomeView:(id)sender
{
	if (![NSThread isMainThread]) {
		[self performSelectorOnMainThread:@selector(showHomeView:) withObject:nil waitUntilDone:NO];
		return;
	}
	
	HomeViewController *homeVC = [HomeViewController sharedViewController];
	[self.navigationController presentModalViewController:homeVC animated:YES];
}

@end

